package cn.marinda.resturants.service.impl;

import cn.marinda.resturants.entity.Bill;
import cn.marinda.resturants.entity.Identity;
import cn.marinda.resturants.global.Pages;
import cn.marinda.resturants.mapper.BillMapper;
import cn.marinda.resturants.service.BillService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

/**
 * 账单服务impl
 *
 * @author hwj
 * @date 2021/12/20
 */
@Service("billService")
public class BillServiceImpl extends ServiceImpl<BillMapper,Bill> implements BillService {


    /**
     * 选择由uid
     *
     * @param uid uid
     * @return {@link Bill}
     */
    @Override
    public Bill selectByUid(String uid) {
        return baseMapper.selectById(uid);
    }

    /**
     * 添加一个账单
     *
     * @param bill 账单
     * @date 2021/12/1
     */
    @Override
    public boolean addBill(Bill bill) {
        bill.setUid(UUID.randomUUID().toString());
        return baseMapper.insert(bill)>0;
    }

    /**
     * 删除账单
     *
     * @param uid uid
     * @date 2021/12/1
     * @version 1.0
     */
    @Override
    public boolean deleteBill(String uid) {
        return baseMapper.deleteById(uid)>0;
    }

    /**
     * 查询一个admin_uid的账单
     *
     * @param uid uid
     * @return
     */
    @Override
    public List<Bill> selectByAdminUid(String uid) {
        List<Bill> billList = baseMapper.selectAdminUidList(uid);
        return billList;
    }

    /**
     * 查询所有type属性下的账单
     *
     * @param type 属性类型
     * @return 集合类型
     */
    @Override
    public List<Bill> selectTypeList(String type) {
        return baseMapper.selectTypeList(type);
    }

    /**
     * number数
     *
     * @param adminUid
     * @return
     */
    @Override
    public BigDecimal selectBillNumber(String adminUid) {
        return baseMapper.selectBillNumber(adminUid);
    }

    /**
     * 乐观锁修改账单
     *
     * @param bill 账单
     * @return 结果
     */
    @Override
    public boolean updateBill(Bill bill) {
        Bill sqlBill = baseMapper.selectById(bill.getUid());
        if(sqlBill == null){
            return false;
        }
        bill.setVersion(sqlBill.getVersion());
        return lambdaUpdate().eq(Bill::getUid,bill.getUid()).eq(Bill::getVersion,bill.getVersion()).set(Bill::getVersion,bill.getVersion()+1).update(bill);
    }

    /**
     * 返回Bill列表
     *
     * @return 账单列表
     */
    @Override
    public List<Bill> queryList() {
        return baseMapper.selectList(null);
    }

    /**
     * 返回Bill列表
     *
     * @param pages
     * @return 账单列表
     */
    @Override
    public IPage<Bill> selectBillPage(Pages<?> pages) {
        return baseMapper.selectBillPage(pages);
    }
}
